var express = require('express');
var router = express.Router();
var http =require("http");
var https =require("https");
var url = require('url');
var Iconv = require('iconv-lite');
var md5 = require('md5');

var MongoClient = require("./../mongodb/MongoClient");
var MySql = require("./../mongodb/MySql");

/* GET home page. */
router.get('/', function(req, res, next) {
  var obj = url.parse(req.url, true).query;
	console.log(obj.newVal)
	var options = {
		hostname:"sp0.baidu.com",
		port:443,
		path:"/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd="+obj.newVal,
		methods:"GET"
	}
	
	var reqData = https.request(options , function (mydata) {
			var str = "";
			mydata.on('data' ,(data) => {
				str += data;
				console.log(eval(str.split("sug")[1]).s)
			})
			mydata.on('err', () => {
				console.log('err');
			})
			mydata.on('end', () => {
				res.send(eval(str.split("sug")[1]).s);
//				console.log(str);
			})
	})
		reqData.on("error", () => {
 		  console.log("err")
			 })
	reqData.end();
});


//影片详情
router.get('/detail', function(req, res, next) {
	var obj = url.parse(req.url, true).query;
	console.log(obj.movieId)
	var options = {
		hostname:"m.mtime.cn",
		port:80,
		path:"/Service/callback.mi/movie/Detail.api?movieId="+obj.movieId+"&locationId=292&t=201711394746349",
		methods:"GET"
	}
	
	var reqData = http.request(options , function (mydata) {
			var str = "";
			mydata.on('data' ,(data) => {
				str += data;
//				console.log(str)
			})
			mydata.on('err', () => {
				console.log('err');
			})
			mydata.on('end', () => {
				res.send(str);
//				console.log(str);
			})
	})
		reqData.on("error", () => {
 		  console.log("err")
			 })
	reqData.end();
});


//热门话题
router.get('/list', function(req, res, next) {
	var obj = url.parse(req.url, true).query;
	console.log(obj.movieId)
	var options = {
		hostname:"m.mtime.cn",
		port:80,
		path:"/Service/callback.mi/Movie/HotLongComments.api?movieId="+obj.movieId+"&pageIndex=1&t=201711219105617478",
		methods:"GET"
	}
	
	var reqData = http.request(options , function (mydata) {
			var str = "";
			mydata.on('data' ,(data) => {
				str += data;
//				console.log(str)
			})
			mydata.on('err', () => {
				console.log('err');
			})
			mydata.on('end', () => {
				res.send(str);
//				console.log(str);
			})
	})
		reqData.on("error", () => {
 		  console.log("err")
			 })
	reqData.end();
});

//影评
//http://m.mtime.cn/Service/callback.mi/Showtime/MovieComments.api?movieId=211978&pageIndex=1&t=2017115928411864

router.get('/listassess', function(req, res, next) {
	var obj = url.parse(req.url, true).query;
	console.log(obj.movieId)
	var options = {
		hostname:"m.mtime.cn",
		port:80,
		path:"/Service/callback.mi/Showtime/MovieComments.api?movieId="+obj.movieId+"&pageIndex=1&t=2017115928411864",
		methods:"GET"
	}
	
	var reqData = http.request(options , function (mydata) {
			var str = "";
			mydata.on('data' ,(data) => {
				str += data;
//				console.log(str)
			})
			mydata.on('err', () => {
				console.log('err');
			})
			mydata.on('end', () => {
				res.send(str);
//				console.log(str);
			})
	})
		reqData.on("error", () => {
 		  console.log("err")
			 })
	reqData.end();
});


//登陆
router.get('/login',function (req,res ,next) {
	var loginObj = url.parse(req.url, true).query;
	
	loginObj.password = md5(loginObj.password);
	console.log(loginObj.password)
	MongoClient.mongoConnect('pull', function(db) {
//		console.log(111)
		MySql.findData(db,'users',loginObj,{},function(result){
//			console.log(result)
			if(result.length == "0"){
				res.send("0")
				db.close();
			}else{
				res.send("1");
				db.close();
			}
		})
	},function(err){
		console.log(err);
	})
})

//注册
router.get('/reg',function (req,res ,next) {
	var registerObj = url.parse(req.url, true).query;
	registerObj.password = md5(registerObj.password);
//	console.log(registerObj)
	MongoClient.mongoConnect('pull', function(db) {
		console.log('数据库链接成功')
		MySql.findData(db,'reg',registerObj,{},function(result){
//			console.log(result)
			if(result.length == "0"){//表示没有注册
				MySql.insertOneData(db,'reg',registerObj,function(result){
					console.log('插入成功')
					res.send('1');
					db.close();
				})
			}else{//表示已经注册过
				res.send("0");
				db.close();
			}
		})
	},function(err){
		console.log(err);
	})
})


module.exports = router;
